﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SecurityApi.DataAccess
{
    public class UserRoleRepository : BaseRepository
    {

        public UserRoleRepository()
            : base()
        {
        }

        public UserRoleRepository(SecurityApiEntities context)
            :base(context)
        {
        }

        public IQueryable<SecurityApi.Model.UserRole> GetUserRolesInEnterprise(int userId, int enterpriseId)
        {
            var query = from u in _context.UserRoles
                        join r in _context.Roles on u.RoleId equals r.RoleId
                        join a in _context.Applications on r.ApplicationId equals a.ApplicationId
                        where u.UserId == userId && a.EnterpriseId == enterpriseId
                        select new SecurityApi.Model.UserRole
                        {
                            UserRoleId = u.UserRoleId,
                            ApplicationId = r.ApplicationId,
                            RoleId = r.RoleId,
                            UserId = u.UserId,
                            RoleName = r.RoleName
                        };

            return query;

        }


        public IQueryable<SecurityApi.Model.UserRole> GetUserRoles(int userId, int applicationId)
        {

            var query = from u in _context.UserRoles
                        join r in _context.Roles on u.RoleId equals r.RoleId
                        where u.UserId == userId && r.ApplicationId == applicationId
                        select new SecurityApi.Model.UserRole
                        {
                            UserRoleId = u.UserRoleId,
                            ApplicationId = r.ApplicationId,
                            RoleId = r.RoleId,
                            UserId = u.UserId,
                            RoleName = r.RoleName
                        };
            
            return query;
        }

        public IQueryable<SecurityApi.Model.UserRole> GetUserRoles(string userName, int applicationId)
        {

            var query = from u in _context.UserRoles
                        join r in _context.Roles on u.RoleId equals r.RoleId
                        join e in _context.EnterpriseUsers on u.UserId equals e.UserId
                        where e.UserName == userName && r.ApplicationId == applicationId
                        select new SecurityApi.Model.UserRole
                        {
                            UserRoleId = u.UserRoleId,
                            ApplicationId = r.ApplicationId,
                            RoleId = r.RoleId,
                            UserId = u.UserId,
                            RoleName = r.RoleName
                        };

            return query;
        }

        public int AddUserRole(int userId, int roleId)
        {
            int rowId = -1;

            try
            {
                UserRole role = new UserRole();
                role.UserId = userId;
                role.RoleId = roleId;
                _context.UserRoles.Add(role);
                _context.SaveChanges();
                rowId = role.UserRoleId;
            }
            catch (Exception ex)
            {
                rowId = -1;
            }

            return rowId;
        }

        public bool RemoveUserRole(int id)
        {
            bool bResult = true;

            try
            {
                UserRole userRole = _context.UserRoles.Where(p => p.UserRoleId == id).SingleOrDefault();
                if (null != userRole)
                {
                    _context.UserRoles.Remove(userRole);
                    _context.SaveChanges();
                }
                else
                {
                    bResult = false;
                }
            }
            catch (Exception ex)
            {
                bResult = false;
            }

            return bResult;
        }

    }
}
